<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\UserRequest;
use App\Notifications\CommentReplied;
use App\Notifications\TopicReplied;
use App\Transformers\UserTransformer;
use App\Http\Resources\UserResource;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\User;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = User::where("key", $id)->firstOrFail();
        $userResource = new UserResource($user);

        $additional = \request('additional', []);
        if (!is_array($additional)) {
            $additional = explode(',', $additional);
        }
        $additionalArr = [];
        if (in_array("stat_counts", $additional)) {
            $notificationCount = $user->unreadNotifications()->count();
            $additionalArr["stat_counts"] = ['notification_count' => $notificationCount];
        }

        $userResource->additional($additionalArr);

        return normalize(0, 'OK', $userResource);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UserRequest $request, $id)
    {
        $user = User::where('key', $id)->firstOrFail();
        $update = $request->only(['name', 'avatar']);
        $result = $user->update($update);

        return api(RET_OK, MSG_OK, [$result]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
